# -*- coding:utf-8 -*-
import xlrd
import xlwt
from pyExcelerator import *
from xlutils.copy import copy
from openpyxl import load_workbook
import re
import sys
reload(sys)
sys.setdefaultencoding('utf8') 
filecs="HexArray.cs"
filenamecs= open(filecs,'a')

def hex2arr( hexfile1, hexfile2, hexfile3): 
       
    hex1= open(hexfile1,'r')
    hex2= open(hexfile2,'r')
    hex3= open(hexfile3,'r')
    seg_no = 0
    flashAddr = [0x00B00000,0x00B00100,0x00B00200,0x00B00400,0x00B00900,0x00B01CA0,0x00C16000,0x007B0000]   
    FileSize = [0x100, 0x80, 0x200, 0x400, 0x100, 0x144, 0xBA000, 0x30000]
    flash_checksum = [0, 0, 0, 0, 0, 0, 0, 0]
    print>>filenamecs, "namespace WinCANTool"
    print>>filenamecs, "{"
    print>>filenamecs, "    public partial class FlashWindow1319"  
    print>>filenamecs, "    {"    
    print>>filenamecs, "        public byte[][] SegData;"
    print>>filenamecs, "        public int[] FileSize;"   
    print>>filenamecs, "        public int[] flashAddr;" 
    print>>filenamecs, "        public ushort[] flash_checksum;" 
    print>>filenamecs, "        public void InitSegData(  )" 
    print>>filenamecs, "        {"
    print>>filenamecs, "            FileSize = new int[8];"
    print>>filenamecs, "            FileSize[0] = 0x100;"
    print>>filenamecs, "            FileSize[1] = 0x80;"
    print>>filenamecs, "            FileSize[2] = 0x200;"
    print>>filenamecs, "            FileSize[3] = 0x400;"
    print>>filenamecs, "            FileSize[4] = 0x100;"
    print>>filenamecs, "            FileSize[5] = 0x144;"
    print>>filenamecs, "            FileSize[6] = 0xBA000;"
    print>>filenamecs, "            FileSize[7] = 0x30000;"
    
    print>>filenamecs, "            flash_checksum = new ushort[8];"
    
    print>>filenamecs, "            flashAddr = new int[8];"
    print>>filenamecs, "            flashAddr[0] = 0x00B00000;"
    print>>filenamecs, "            flashAddr[1] = 0x00B00100;"
    print>>filenamecs, "            flashAddr[2] = 0x00B00200;"
    print>>filenamecs, "            flashAddr[3] = 0x00B00400;"
    print>>filenamecs, "            flashAddr[4] = 0x00B00900;"
    print>>filenamecs, "            flashAddr[5] = 0x00B01CA0;"
    print>>filenamecs, "            flashAddr[6] = 0x00C16000;"
    print>>filenamecs, "            flashAddr[7] = 0x007B0000;"
    
    print>>filenamecs, "            SegData = new byte[8][];"
    print>>filenamecs, "            SegData[0] = new byte[0x100];"
    print>>filenamecs, "            SegData[1] = new byte[0x80];"
    print>>filenamecs, "            SegData[2] = new byte[0x200];"
    print>>filenamecs, "            SegData[3] = new byte[0x400];"
    print>>filenamecs, "            SegData[4] = new byte[0x100];"
    print>>filenamecs, "            SegData[5] = new byte[0x144];"
    print>>filenamecs, "            SegData[6] = new byte[0xBA000];"
    print>>filenamecs, "            SegData[7] = new byte[0x30000];"

    print>>filenamecs, "            SegData[%d] = new byte[0x%x]" %(seg_no, FileSize[seg_no])
    print>>filenamecs, "            {"      
  
    for LineData in hex1:

        index = 1
        len = int(LineData[index:index+2],16)
        
        index += 2
        addr = int(LineData[index:index+4],16)
        
        index += 4
        section = int(LineData[index:index+2],16)
        
        index += 2
        
        if (section == 0x00):

            if (addr >= (flashAddr[seg_no+1] & 0xffff)):            
                print>>filenamecs, "            }; // seg_no=%d, startaddr=0x%x, size=0x%x" % (seg_no, flashAddr[seg_no], FileSize[seg_no])  
                seg_no += 1
                #FileSize[seg_no] = 0
                print>>filenamecs, "            SegData[%d] = new byte[0x%x]" %(seg_no, FileSize[seg_no])
                print>>filenamecs, "            {"
            hex_data = ''
            for l in range(0, len):
                hex_data += "0x" +LineData[index:index+2]+','
                flash_checksum[seg_no] += int(LineData[index:index+2],16);
                #FileSize[seg_no] += 1
                index += 2
                
            print>>filenamecs, "                %s"       % (hex_data)    
              
    print>>filenamecs, "            }; // seg_no=%d, startaddr=0x%x, size=0x%x" % (seg_no, flashAddr[seg_no], FileSize[seg_no])  


    seg_no = 6
    print>>filenamecs, "            SegData[%d] = new byte[0x%x]" %(seg_no, FileSize[seg_no])
    print>>filenamecs, "            {"
    for LineData in hex2:

        index = 1
        len = int(LineData[index:index+2],16)
        
        index += 2
        addr = int(LineData[index:index+4],16)
        
        index += 4
        section = int(LineData[index:index+2],16)
        
        index += 2
        
        if (section == 0x00):
            hex_data = ''
            for l in range(0, len):
                hex_data += "0x" +LineData[index:index+2]+','
                flash_checksum[seg_no] += int(LineData[index:index+2],16);
                #FileSize[seg_no] += 1
                index += 2
                
            print>>filenamecs, "                %s"       % (hex_data)                  
             
    print>>filenamecs, "            }; // seg_no=%d, startaddr=0x%x, size=0x%x" % (seg_no, flashAddr[seg_no], FileSize[seg_no])  


    seg_no = 7
    print>>filenamecs, "            SegData[%d] = new byte[0x%x]" %(seg_no, FileSize[seg_no])
    print>>filenamecs, "            {"
    for LineData in hex3:

        index = 1
        len = int(LineData[index:index+2],16)
        
        index += 2
        addr = int(LineData[index:index+4],16)
        
        index += 4
        section = int(LineData[index:index+2],16)
        
        index += 2
        
        if (section == 0x00):
            hex_data = ''
            for l in range(0, len):
                hex_data += "0x" +LineData[index:index+2]+','
                flash_checksum[seg_no] += int(LineData[index:index+2],16);
                #FileSize[seg_no] += 1
                index += 2
                
            print>>filenamecs, "                %s"       % (hex_data)    
                 
    print>>filenamecs, "            }; // seg_no=%d, startaddr=0x%x, size=0x%x" % (seg_no, flashAddr[seg_no], FileSize[seg_no])  
    for i in range(0,8):
        print>>filenamecs, "            flash_checksum[%d] = 0x%x;" %(i, flash_checksum[i] & 0xffff)
    
    print>>filenamecs, "        }"

    print>>filenamecs, "    }"
    print>>filenamecs, "}"
if __name__ == '__main__':

    filenamecs= open(filecs,'w')
    filenamecs = open(filecs,'a');    
    hex2arr("121729233562.hex","121729232026.hex","121729222840.hex")


    
    
    
